//
//  ST_IgnoreLookViewController.swift
//  btvideo
//
//  Created by mac on 2020/8/7.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SwiftyJSON
import HandyJSON
import MBProgressHUD
import MJRefresh


class ST_IgnoreLookViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        automaticallyAdjustsScrollViewInsets = false
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
   
        
        tableView.backgroundColor = Color.white
        
        self.request_data()
        
    }
        
    func request_data(){
        
        currentPage += 1
        
        JVrequest(PH.ignoreLikeListApi,parameters:["page":currentPage,"size":15], success: { (json) in
            
                let array = json["data"]["list"].arrayValue
                
                print("忽略的看过Array:",array)
            
                 if self.currentPage == 1{
                    self.data_array = array
                 }else{
                    self.data_array += array
                }
                
            
            self.tableView.reloadData()
            
            self.load_more_option_style(tableView: self.tableView, count: array.count, pageSize: 15) {
                
                self.tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {[weak self] in

                    self?.request_data()
                })
            }
                
            self.tableView.mj_footer?.endRefreshing()
            
        }) { (code, err) in
            self.currentPage -= 1
            
            self.tableView.mj_footer?.endRefreshing()
            
            ToastHud(text: err, view: self.view)
        }
        
    }
    
//    MARK:- Properties
    
    var currentPage = 0
    
    fileprivate lazy var tableView:UITableView = {
        
        let control = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        
        let bottomEdge = iPhoneXSeries ? BTSafeAreaBottom + 10 : 30
        
        control.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: bottomEdge, right: 0)
        
        control.delegate = self
        
        control.dataSource = self
        
//        control.rowHeight = UITableView.automaticDimension
        
//        control.estimatedRowHeight = 92
        
        control.register(cellWithClass: ST_RefuseLookCell.self)
        
        control.separatorStyle = .none
        
        control.allowsMultipleSelectionDuringEditing = true
        
        control.backgroundColor = Color.white
        //        control.backgroundColor = Color.random
        
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        
        control.emptyDataSetSource = self
        control.emptyDataSetDelegate = self
        
        return control
    }()
    
    
    fileprivate var data_array = [JSON]()
    
    fileprivate var first = true
}


extension ST_IgnoreLookViewController:DZNEmptyDataSetSource,DZNEmptyDataSetDelegate{
    
    func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    
    func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
        
        if first{
            self.first = false
            return false
        }else{
            return true
        }
    }
    
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        
        let attrString = NSMutableAttributedString(string: "暂时没有忽略的看过哦")
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1)]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        return attrString
    }
    
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        
        return ST_ImageUtils.imageName("喜欢_img_拒绝的喜欢(空)")
    }
    
}


extension ST_IgnoreLookViewController:UITableViewDelegate,UITableViewDataSource{
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data_array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withClass: ST_RefuseLookCell.self, for: indexPath)
        
        cell.bind_model(json: data_array[indexPath.row])

//        cell.acceipt_closure = {[weak self] m in
//            self?.re_love_handle(json: m)
//        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = data_array[indexPath.item]["accid"].stringValue
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
    }
    
}


//MARK: - Event
extension ST_IgnoreLookViewController{
    
    fileprivate func re_love_handle(json:JSON){
        
        let info = ST_MatchUserInfo.init(accid: json["accid"].stringValue, avatar: json["avatar"].stringValue, nickname: json["nickname"].stringValue)
        
        JVrequest(PH.receiveAgainPersonApi,parameters: ["target_accid":info.accid] ,success: { (json) in
            
            print("喜欢某个用户:\(json.rawString() ?? "空数据")")
            
            if json["data"]["mutual_like"].boolValue{
                ST_BTBootController.shared.matchSuccess(info: info)
            }
            
            self.data_array.removeFirst { (itemJson) -> Bool in
                return itemJson["accid"].stringValue == info.accid
            }
            
            self.tableView.reloadData()
            
        }) { (code, err) in
            
            print("当前 返回喜欢不喜欢 错误信息：",err)
            
            ToastHud(text: err, view: self.view)
        }
        
    }
    
}
